﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using YFTFunction;
using YFTOpenAPI;
using YFTOpenAPI.Model;
namespace YY.Web.Handler
{
    /// <summary>
    /// SmsHandler 的摘要说明
    /// </summary>
    public class SmsHandler : BaseFunction.LoginHelp, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var action = BaseFunction.RequestValidate.GetQueryString("action", "");
            switch (action)
            {
                case "getcode":
                    string mobil = BaseFunction.RequestValidate.GetFormString("mobile", "");
                    context.Response.Write(GetCodeByMoblil(mobil));
                    break;
            }

        }
        /// <summary>
        /// 获取手机验证码
        /// </summary>
        private string GetCodeByMoblil(string mobile)
        {
            //Random rad = new Random();//实例化随机数产生器rad；
            //string value = rad.Next(100000, 1000000).ToString();//用rad生成大于等于100000，小于等于999999的随机数；
            //string MessageInfor = "保证金缴纳，请求的验证码是：" + value + ".验证时间为15分钟,请尽快完成验证；请勿向任何人提供您收到的短信校验码。";
            //string status = "";
            //int flag = 1;
            //if (!BaseFunction.CommonClass.SendMessage(mobile, MessageInfor))
            //{
            //    status = "fail";
            //    flag = 0;
            //}
            //AddSms(mobile, "缴纳保证金,手机验证!", flag);
            //return value + status;
            /*通过POST请求服务*/
            string status = "";
            string ip = BaseFunction.StaticTableClass.SystemConfigHelp.GetSystemConfigValue("YFT_IP");
            var smscode = "";
            var key = ConfigurationManager.AppSettings["YYAgent"];
            var emobile = BaseFunction.DEncrypt.DEncrypt.Encrypt(mobile);
            var md5 = OpenAPI.MD5Encrypt(emobile,key);
            string result = WebRequestHelp.PostDataToUrl("emobile=" + emobile +"&key=" + key + "&md5=" + md5, ip + "/HTTP/YFTAPI.aspx");
            ResponseData responseinfor = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseData>(result);
            int code = responseinfor.statuscode; //1 成功 0失败
            string messageinfor = responseinfor.message; //提示信息  创建失败返回错误原因。
            if (code == 1)
            {
                smscode = responseinfor.data + "";
            }
            else
            {
                status = messageinfor;
            }
            return smscode+status;
        }
        /// <summary>
        /// 加入日志
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        /// <param name="issuccess"></param>
        private void AddSms(string mobile, string content, int issuccess)
        {
            YY.Model.SmsInfor model = new Model.SmsInfor();
            model.SendTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            model.SendContent = content;
            model.PhoneNumber = mobile;
            model.IfSuccess = issuccess;
            BaseFunction.StaticTableClass.SmsInforHelp.bll_SmsInfor.Add(model);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}